package setting.huaqin.com.systemuiitemselect;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.w3c.dom.Text;

/**
 * Created by lubo on 17-12-8.
 */
public class TileView extends RelativeLayout {

    private final static String TAG="TileView";
    private String mName;
    private int mIcon;
    private TextView mTv;
    private ImageView mIV;
    private int mId;

    public TileView(Context context) {
        super(context);
        LayoutInflater.from(context).inflate(R.layout.tile_view,this);
        mTv=(TextView)findViewById(R.id.tile_text);
        mIV=(ImageView)findViewById(R.id.tile_icon);
    }

    public TileView(Context context, AttributeSet attrs) {
        super(context, attrs);
        getAttrs(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.tile_view,this);
        mTv=(TextView)findViewById(R.id.tile_text);
        mIV=(ImageView)findViewById(R.id.tile_icon);
        mTv.setText(mName);
        mIV.setBackgroundResource(mIcon);
    }

    public TileView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public TileView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    private void getAttrs(Context context, AttributeSet attrs) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.tileViewAttr);
        mName = ta.getString(R.styleable.tileViewAttr_tilename);
        mIcon = ta.getResourceId(R.styleable.tileViewAttr_tileicon, R.mipmap.ic_launcher);
        ta.recycle();
    }

    public void setTileIcon(int id){
        mIV.setBackgroundResource(id);
        mId=id;
    }

    public void setTileTitle(String title){
        mTv.setText(title);
    }

    public String getTileTitle(){
        return mTv.getText().toString();
    }

    public int getIconId(){
        return mId;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(event.getAction()==MotionEvent.ACTION_DOWN){
            //this.setVisibility(INVISIBLE);
            Log.e(TAG,"down====>>>>>"+mTv.getText());
        }
        if(event.getAction()==MotionEvent.ACTION_UP){
            //this.setVisibility(VISIBLE);
            Log.e(TAG, "up====>>>>>" + mTv.getText());

        }
        if(event.getAction()==MotionEvent.ACTION_MOVE){
            Log.e(TAG, "move====>>>>>" + mTv.getText());
        }
        if(event.getAction()==MotionEvent.ACTION_CANCEL){
            Log.e(TAG, "ACTION_CANCEL====>>>>>" + mTv.getText());
        }
        return super.onTouchEvent(event);
    }
}
